﻿using System;
using T.FAS.Runtime.Metadata.SPI;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.Kernel.Service;
using T.FAS.Runtime.Metadata.Dao;

namespace T.FAS.Runtime.Metadata.Client
{
    /// <summary>
    /// 元数据客户端服务
    /// </summary>    
    [SGService]
    public class MetaDataClientService: IMetaDataClientService
    {
        private static MetaDataClientService instance;

        private static object syncObj = new object();

        public RefSchemaService RefSchemaServ { get; private set; }

        private MetaDataClientService()
        {
            RefSchemaServ = RefSchemaService.Instance;
        }

        public static MetaDataClientService GetInstance()
        {
            if (instance == null)
            {
                lock (syncObj)
                {
                    if (instance == null)
                    {
                        instance = new MetaDataClientService();
                    }
                }
            }

            return instance;
        }

        /// <summary>
        /// 获取元数据(缓存模式)
        /// </summary>
        /// <param name="metaDataId">元数据ID</param>
        /// <returns>元数据</returns>        
        [SGService(ServiceID = "FASR-CommonService")]
        public IMetaData GetMetaData(string metaDataId)
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MetaDataClientService.GetMetaData.metaDataId");

            return MetaDataCacheCoordinator.GetSingleton().GetMetaData(metaDataId);           
        }
        /// <summary>
        /// 获取元数据(缓存模式)
        /// </summary>
        /// <param name="metaDataId">元数据ID</param>
        /// <returns>元数据</returns>
        public IMetaData GetMetaDataByCode(string metaDataId, string metaTypeId)
        {
            if (string.IsNullOrWhiteSpace(metaDataId) || string.IsNullOrWhiteSpace(metaTypeId))
                throw new ArgumentNullException("MetaDataClientService.GetMetaData.metaDataCode");

            return MetaDataCacheCoordinator.GetSingleton().GetMetaDataByCode(metaDataId, metaTypeId);
        }

        /// <summary>
        /// 查看获取具体的元数据(缓存模式)
        /// </summary>
        /// <param name="metaDataId">元数据ID</param>
        /// <returns>元数据</returns>
        public IMetaDataContent GetMetaDataContent(string metaDataId)
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MetaDataClientService.GetMetaDataContent.metaDataId");

            var metaData = GetMetaData(metaDataId);

            if (metaData == null) return null;

            return metaData.Content;
        }

        /// <summary>
        /// 查看获取具体的元数据(缓存模式)
        /// </summary>
        /// <param name="metaDataId">元数据ID</param>
        /// <returns>元数据</returns>
        public string GetMetaDataWithNoDeSerialize(string metaDataId)
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MetaDataClientService.GetMetaDataWithNoDeSerialize.metaDataId");
            return MetaDataCacheCoordinator.GetSingleton().GetMetaDataWithNoDeSerialize(metaDataId);
        }
        /// <summary>
        /// 获取实际的元数据(缓存模式)
        /// </summary>
        /// <typeparam name="T">元数据类型</typeparam>
        /// <param name="metaDataId">元数据ID</param>
        /// <returns>元数据</returns>
        public T GetMetaData<T>(string metaDataId) where T : class, IMetaDataContent
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MetaDataClientService.GetMetaDataContent.metaDataId");

            var metaData = GetMetaData(metaDataId);

            if (metaData == null) return default(T);

            return metaData.Content as T;
        }

        //public string GetCurrentEnvironment()
        //{
        //    return EnvironmentManager.CurrentEnvironment;
        //}

        #region 未用到
        /// <summary>
        /// 获取元数据(HSF服务模式)
        /// </summary>
        /// <param name="metaDataId">元数据ID</param>
        /// <returns>元数据</returns>
        public IMetaData GetMetaDataFromHSF(string metaDataId)
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MetaDataClientService.GetMetaDataFromHSF.metaDataId");

            return MetaDataHSFService.GetMetaData(metaDataId);
        }

        /// <summary>
        /// 查看获取具体的元数据(HSF服务模式)
        /// </summary>
        /// <param name="metaDataId">元数据ID</param>
        /// <returns>元数据</returns>
        public IMetaDataContent GetMetaDataContentFromHSF(string metaDataId)
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MetaDataClientService.GetMetaDataContentFromHSF.metaDataId");

            var metaData = GetMetaDataFromHSF(metaDataId);

            if (metaData == null) return null;

            return metaData.Content;
        }

        /// <summary>
        /// 获取实际的元数据(HSF服务模式)
        /// </summary>
        /// <typeparam name="T">元数据类型</typeparam>
        /// <param name="metaDataId">元数据ID</param>
        /// <returns>元数据</returns>
        public T GetMetaDataFromHSF<T>(string metaDataId) where T : class, IMetaDataContent
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MetaDataClientService.GetMetaDataFromHSF.metaDataId");

            var metaData = GetMetaDataFromHSF(metaDataId);

            if (metaData == null) return default(T);

            return metaData.Content as T;
        }
        #endregion
    }
}
